package com.one.group.admin.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.one.group.model.dto.DecorationQueryDto;
import com.one.group.result.Result;
import com.one.group.model.dto.DecorationApplicationDto;
import com.one.group.model.dto.DecorationDetailDto;
import com.one.group.model.dto.DecorationManagementQueryDto;
import com.one.group.model.entity.DecorationManagement;
import com.one.group.model.vo.DecorationManagementListVo;
import com.one.group.service.DecorationChargeService;
import com.one.group.service.DecorationManagementService;
import com.one.group.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author kld
 * @date 2025/10/1
 * @description 装修管理Controller
 */
@RestController
@RequestMapping("/decorationManagement")
public class DecorationManagementController {

    @Autowired
    private DecorationManagementService decorationManagementService;
    @Autowired
    private DecorationChargeService decorationChargeService;

    /**
     * 查询装修管理列表
     */
    @PostMapping("/list")
    public Result list(@RequestBody DecorationManagementQueryDto queryDto) {

        // 设置分页条件
        PageHelper.startPage(queryDto.getPage(), queryDto.getLimit());

        // 查询数据
        List<DecorationManagementListVo> list = decorationManagementService.findDecorationListWithCellName(queryDto);

        // 封装分页结果
        PageInfo<DecorationManagementListVo> pageInfo = new PageInfo<>(list);

        return ResultUtil.success(pageInfo);}


    /**
     * 关键词查询keyword
     */
    @PostMapping("/findByKeyword")
    public Result<List<DecorationManagementListVo>> findByKeyword(@RequestBody DecorationQueryDto queryDto) {
        List<DecorationManagementListVo> list = decorationManagementService.findDecorationListByQueryDto(queryDto);
        return list != null ? new Result<>(200, list, "success") : new Result<>(500, null, "查询失败");
    }


    /**
     * 新增装修申请（包含收费标准）
     */
    @PostMapping("/add-with-charges")
    public Result<Boolean> addDecorationWithCharges(@RequestBody DecorationApplicationDto applicationDto) {
        boolean success = decorationManagementService.addDecorationWithCharges(applicationDto);
        return success ? new Result<>(200, true, "success") : new Result<>(500, false, "新增失败");
    }

    /**
     * 获取装修详情（包含验收信息和费用信息）
     */
    @GetMapping("/detail-with-info/{id}")
    public Result<DecorationDetailDto> getDecorationDetail(@PathVariable Long id) {
        DecorationDetailDto detail = decorationManagementService.getDecorationDetail(id);

        return detail != null ? new Result<>(200, detail, "success") : new Result<>(500, null, "装修信息不存在");
    }

    /**
     * 新增装修申请
     */
    @PostMapping("/add")
    public Result<Boolean> addDecoration(@RequestBody DecorationManagement decorationManagement) {
        boolean success = decorationManagementService.addDecoration(decorationManagement);
        return success ? new Result<>(200, true, "success") : new Result<>(500, false, "新增失败");
    }
    
    /**
     * 修改装修信息
     */
    @PostMapping("/update")
    public Result<Boolean> updateDecoration(@RequestBody DecorationManagement decorationManagement) {
        boolean success = decorationManagementService.updateDecoration(decorationManagement);
        return success ? new Result<>(200, true, "success") : new Result<>(500, false, "修改失败");
    }

    /**
     * 删除装修信息
     */
    @PostMapping("/delete/{id}")
    public Result<Boolean> deleteDecoration(@PathVariable Long id) {
        boolean success = decorationManagementService.deleteDecoration(id);
        return success ? new Result<>(200, true, "success") : new Result<>(500, false, "删除失败");
    }

    /**
     * 批量删除装修信息
     */
    @PostMapping("/batch-delete")
    public Result<Boolean> batchDelete(@RequestBody List<Long> ids) {
        boolean success = decorationManagementService.batchDelete(ids);
        return success ? new Result<>(200, true, "success") : new Result<>(500, false, "批量删除失败");
    }

    /**
     * 审核装修申请
     */
    @PostMapping("/approve/{id}")
    public Result<Boolean> approveDecoration(
            @PathVariable Long id, 
            @RequestParam String status, 
            @RequestParam String approver) {
        boolean success = decorationManagementService.approveDecoration(id, status, approver);
        return success ? new Result<>(200, true, "success") : new Result<>(500, false, "审核失败");
    }

    /**
     * 验收装修
     */
    @PostMapping("/inspect/{id}")
    public Result<Boolean> inspectDecoration(@PathVariable Long id,
                                            @RequestParam String inspector,
                                            @RequestParam String inspectionResult,
                                            @RequestParam(required = false) String inspectionNotes,
                                            @RequestParam(required = false) String inspectionPhotosUrl) {
        boolean success = decorationManagementService.inspectDecoration(id, inspector, inspectionResult, inspectionNotes, inspectionPhotosUrl);
        return success ? new Result<>(200, true, "success") : new Result<>(500, false, "验收失败");
    }
    



    

    

}